package com.seeTools.next.common.core;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Author: seeTools
 * @Date: 2023/9/2  8:22
 * @Version 1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 消息
     */
    private String msg;

    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    /**
     * 数据
     */
    private T data;


    public static <T> Result<T> success() {
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        return result;
    }

    public static <T> Result<T> success(String msg) {
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> error() {
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.ERROR.getCode());
        result.setMsg(ResultCode.ERROR.getMsg());
        return result;
    }

    public static <T> Result<T> error(String msg) {
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.ERROR.getCode());
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> error(String msg, T data) {
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.ERROR.getCode());
        result.setMsg(msg);
        result.setData(data);
        return result;
    }


    public static <T> Result<T> state(boolean b) {
        return b ? Result.success() : Result.error();
    }

    public static <T> Result<T> state(T b) {
        return b != null ? success(b) : ok(ResultCode.ISNULL);
    }

    public static <T> Result<T> ok(ResultCode resultCode) {
        Result<T> result = new Result<T>();
        result.setCode(resultCode.getCode());
        result.setMsg(resultCode.getMsg());
        return result;
    }

    /**
     * 返回数据
     *
     * @param data 数据
     * @return 统一返回类
     */
    public Result<T> data(T data) {
        this.setData(data);
        return this;
    }

}
